package org.koyad.portal.tags;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.koyad.portal.constants.FrameworkConstants;
import org.koyad.portal.context.PistonContext;
import org.koyad.portal.context.PistonRequest;
import org.koyad.portal.exceptions.ErrorCodes;
import org.koyad.portal.exceptions.FrameworkException;
import org.koyad.portal.tags.piston.PageFragmentTag;
import org.koyad.portal.tags.piston.TileTag;
import org.koyad.portal.vo.PageMetaData;

public class ViewRoot {

	private PageMetaData _metadata = null;
	
	//this will hold theme tree
	private AbstractTag _theme = null;
	
	//this will hold portal/normal page tree
	private AbstractTag _page = null;
	
	private Map<TileTag, PageFragmentTag> _tileContents = new HashMap<TileTag, PageFragmentTag>();
	private boolean _validationFailed = false;
	private boolean _session = true;
	private List<String> _errors = new ArrayList<String>();
	
	//required for accessing tag store from SimpleContext
	private AbstractTag _currentTag = null;
	
	//During tree creation this will be used to set current tile content
	private TileTag _currentTile = null;
	
	public String getCurrentTileId() {
		if(null != _currentTile) {
			return _currentTile.getAttributeValue(FrameworkConstants.PISTON_TILE_ID);
		}
		return null;
	}
	
	
	public void setCurrentTile(TileTag tile) {
		if (null == _currentTile) {
			_currentTile = tile;
		} else {
			throw new FrameworkException(ErrorCodes.FWKPC001);
		}
	}

	public void resetCurrentTile(TileTag tile) {
		if (_currentTile.equals(tile)) {
			_currentTile = null;
		} else {
			throw new FrameworkException(ErrorCodes.FWKPC002);
		}
	}
	
	public void setCurrentTile() {
		if (null == _currentTile) {
			for(TileTag tile : _tileContents.keySet()) {
				String reqTileId = ((PistonRequest)PistonContext.getContext().getRequest()).getReqTileId();
				if(tile.getAttributeValue(FrameworkConstants.PISTON_TILE_ID).equals(reqTileId)) {
					_currentTile = tile;
					break;
				}
			}
		} else {
			throw new FrameworkException(ErrorCodes.FWKPC001);
		}
	}

	public void resetCurrentTile() {
		_currentTile = null;
	}
	
	public void setCurrentTag(AbstractTag tag) {
		_currentTag = tag;
	}
	
	public AbstractTag getCurrentTag() {
		return _currentTag;
	}
	
	public void addTileContent(PageFragmentTag fragment) {
		
		//reset current tile id before rendering phase
		String reqTileId = ((PistonRequest)PistonContext.getContext().getRequest()).getReqTileId();
		if(null != reqTileId) {
			//for post request
			Set<TileTag> tiles = _tileContents.keySet();
			for(TileTag tile : tiles) {
				if(tile.getAttributeValue(FrameworkConstants.PISTON_TILE_ID).equals(reqTileId)) {
					_tileContents.put(tile, fragment);
					break;
				}
			}
		} else {
			_tileContents.put(_currentTile, fragment);
		}
	}
	
	public AbstractTag getTileContent(TileTag tile) {
		return _tileContents.get(tile);
	}
	
	public ViewRoot(PageMetaData metadata) {
		_metadata = metadata;
	}

	public void setTheme(AbstractTag theme) {
		_theme = theme;
	}

	public AbstractTag getPage() {
		return _page;
	}
	
	public void setPage(AbstractTag page) {
		_page = page;
	}

	public boolean isSession() {
		return _session;
	}

	public void setSession(boolean session) {
		_session = session;
	}

	public PageMetaData getMetaData() {
		return _metadata;
	}

	public void setValidationFailed(boolean flag) {
		_validationFailed = flag;
	}

	public boolean validationFailed() {
		return _validationFailed;
	}

	public void addError(String errorCode) {
		_errors.add(errorCode);
	}

	public List<String> getErrors() {
		return _errors;
	}

	public void resetErrors() {
		_errors.clear();
	}

	public void render() {
		if(null != _theme) {
			_theme.render();
		} else {
			_page.render();
		}
	}

	public void validate() {
		if(null != _theme) {
			_theme.validate();
		} else {
			_page.validate();
		}
	}
	
}
